{{--
/**
 * Created by PhpStrom
 * User: 郭碧云
 * Date: 2021/3/24 8:51
 * Desc:
 */
--}}
@extends("layout.admin")

@section("title", "分配角色")

@section("content")

    <style>
        .layui-form-checkbox[lay-skin=primary] {
            height: 25px !important;
        }
    </style>
    <div class="layui-form" lay-filter="layuiadmin-form-role" id="layuiadmin-form-role" style="padding: 20px 30px 0 0;">
        <form class="layui-form" method="post"
              action="{{session('guard')=='system'?route("system.user.role_assign", ['user' => $user->id]): route("admin.user.role_assign", ['user' => $user->id]) }}"
              id="role_form">
            @csrf
            <div class="layui-form-item" style="padding: 0px 39px;">分配用户：{{$user->real_name}} &nbsp;&nbsp;
                <span>账号名称：{{$user->name}}</span> &nbsp;&nbsp; <span style="color: red">(备注：时间不选表示用户拥有角色时间为永久)</span>
            </div>
            <div class="layui-form-item" id="test">
                <ul class="layui-input-block">
                    @foreach($roles as $role)
                        <div class="layui-row" style="margin-bottom: 15px;">
                            <div class="layui-col-xs2">
                                <input type="checkbox" name="roles[]" lay-skin="primary" value="{{$role['name']}}"
                                       @if(!empty($has_role)) @foreach($has_role as $r) @if($r["name"]==$role['name']) checked
                                       @endif @endforeach @endif
                                       title="{{$role['name']}}">
                            </div>
                            <div class="layui-col-xs2">
                                <input type="text" name="expire_at[{{$role['name']}}]" class="layui-input expire_at"
                                       placeholder="{{$role['expire_at'] ?? "有效期:yyyy-MM-dd"}}">
                            </div>
                        </div>
                    @endforeach
                </ul>

            </div>


            @if(session("guard")!="system")
            <div class="layui-form-item" style="padding: 0px 39px;">可管理的组织机构</div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <div id="orgs" name="orgs" style="width: 60%"></div>
                </div>

            </div>

            <div class="layui-form-item" style="padding: 0px 39px;">可管理的科目</div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <div id="categories" name="categories" style="width: 60%"></div>
                </div>
            </div>
            @endif
        </form>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-sm" id="sub">立即提交</button>
                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm"
                        onclick='window.location.href="javascript:history.go(-1)"'>返回
                </button>
            </div>
        </div>
    </div>

@endsection

@section("script")
    <script src="{{asset('layui/modules/xm-select.js')}}"></script>
    <script src="{{asset('js/tree/select.js')}}"></script>
    <script type="text/javascript">
        var idx = -1;
        layui.use(['form', 'jquery', 'laydate','xmSelect'], function () {
            var form = layui.form,
                $ = layui.jquery,
                laydate = layui.laydate;
            //日期时间选择器
            lay('.expire_at').each(function () {
                laydate.render({
                    elem: this
                    , trigger: 'click'
                    , type: 'datetime'
                });
            });

            @if(session("guard")!="system")
            //树形下拉菜单
            var xmSelect = layui.xmSelect;
            let params=selectTreeMoreParams('#orgs','请选择',"",JSON.parse('{!! $orgs !!}'),'select',(res)=>{
                console.log(res)
            },true);
            xmSelect.render(params);

            let category=selectTreeMoreParams('#categories','请选择',JSON.parse('{!! $hasCategory !!}'),JSON.parse('{!! $category !!}'),'categories',(res)=>{},true);
            xmSelect.render(category);
            @endif

            $("#sub").click(function () {
                admin.send($("#role_form").attr("action"), $("#role_form").attr("method"), $("#role_form").serialize());
            });
        });

    </script>
@endsection
